﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine.Rendering.PostProcessing;

public class StepController : MonoBehaviour
{
    public static StepController instance;
    public int currentLv = 1;
    public PostProcessVolume post;
    public Wenduji wenduji;
    public TangchiController tangchi;
    public GameObject qipao;
    public GameObject[] dahanList;
    public GameObject dahan;
    public GameObject txtCountDown;
    public GameObject effectAixin;
    public GameObject water;
    public GameObject sex;
    public UIController uicontroller;
    public Material[] matList;
    public Material matDahan;
    public Material matHan;
    public Material[] hanList;
    public int wenduZhongxin;
    public int wenduRand;
    public int rangeZhongxin;
    public int interval;
    public int wendu, range;
    public GameObject slider;
    public Sprite huang, lv;
    public GameObject effcaidai;
    public GameObject deng;
    public Material sangnafang;
    private Vignette vignette;

    private void Awake()
    {
        Application.targetFrameRate = 30;
        instance = this;
        post.profile.TryGetSettings(out vignette);

    }

    // Use this for initialization
    void Start()
    {
        JoyPac.Instance().InitSDK("AdjustToken", "GAKey", "GASecret");
        JoyPac.Instance().SetLogEnable(true);
        Application.targetFrameRate = 30;
        RestartGame();
    }

    float dahanHot = 0;
    float dahanCold = 0;
    // Update is called once per frame
    float slidervalue = 0;
    void Update()
    {
        if (slidervalue < tangchi.manyidu - 0.05f)
            slidervalue = tangchi.manyidu - 0.05f;
        slider.GetComponent<Slider>().value = slidervalue / 0.47f;
        if (tangchi.manyidu > 0.47f)
        {
            PauseGame();
            StartCoroutine(Shengli());
            currentLv++;
        }
        if (tangchi.isGameStart)
        {
            if (isColdOrHot == 1)
            {
                dahan.GetComponent<Animation>().CrossFade("re", 0.3f);
                if (dahanHot < 1)
                    dahanHot += 0.05f;
                matDahan.SetFloat("_NH", dahanHot);
                matHan.SetFloat("_control", 1);
                matHan.SetFloat("_HOT", 1);
                dahanCold = 0;
                matDahan.SetFloat("_NC", 0);
                vignette.color.value = new Color(1f, 0.3f, 0.2f);
                if (vignette.intensity.value < 0.5)
                    vignette.intensity.value += 0.03f;
            }
            else if (isColdOrHot == -1)
            {
                dahan.GetComponent<Animation>().CrossFade("leng", 0.3f);
                if (dahanCold < 1)
                    dahanCold += 0.05f;
                matDahan.SetFloat("_NC", dahanCold);
                dahanHot = 0;
                matDahan.SetFloat("_NH", 0);
                matHan.SetFloat("_control", 0);
                matHan.SetFloat("_HOT", 0);
                vignette.color.value = new Color(0.2f, 0.7f, 1f);
                if (vignette.intensity.value < 0.5)
                    vignette.intensity.value += 0.03f;
            }
            else if (isColdOrHot == 0)
            {
                if(tangchi.manyidu > 0.46f)
                    dahan.GetComponent<Animation>().CrossFade("shuang", 0.3f);
                else
                    dahan.GetComponent<Animation>().CrossFade("standby", 0.3f);
                if (dahanCold > 0)
                    dahanCold -= 0.1f;
                if (dahanHot > 0)
                    dahanHot -= 0.1f;
                matDahan.SetFloat("_NC", dahanCold);
                matDahan.SetFloat("_NH", dahanHot);
                matHan.SetFloat("_control", 1);
                matHan.SetFloat("_HOT", 0);
                if (vignette.intensity.value > 0)
                    vignette.intensity.value -= 0.1f;
            }
        }
    }

    IEnumerator Shengli()
    {
        JoyPac.Instance().GA_TrackEvent("pass_level", currentLv);
        deng.SetActive(true);
        deng.GetComponent<Animation>().Play("juguangdeng");
        effcaidai.SetActive(true);
        effcaidai.GetComponent<ParticleSystem>().Play();
        sangnafang.DOColor(new Color(0.2f, 0.2f, 0.2f, 1), 1.5f);
        yield return new WaitForSeconds(5);
        effcaidai.SetActive(false);
        deng.SetActive(false);
        sangnafang.SetColor("_Color", new Color(0.8f, 0.8f, 0.8f, 1));
        uicontroller.OpenUIReward();
    }

    public void PauseGame()
    {
        tangchi.Reset();
        vignette.intensity.value = 0f;
        vignette.color.value = Color.white;
        effectAixin.SetActive(false);
        sex.SetActive(false);
        water.SetActive(false);
        countdown = false;
        slider.SetActive(false);
        StopAllCoroutines();
    }

    public void RestartGame()
    {
        dahanHot = 0;
        dahanCold = 0;
        slider.SetActive(true);
        slidervalue = 0;
        slider.GetComponent<Slider>().value = 0;
        matHan.SetFloat("_control", 0);
        matDahan.SetFloat("_NH", 0);
        matDahan.SetFloat("_NC", 0);
        uicontroller.CloseAll();
        water.SetActive(false);
        effcaidai.SetActive(false);
        deng.SetActive(false);
        sangnafang.SetColor("_Color", new Color(0.8f, 0.8f, 0.8f, 1));
        InitParameter();
        AnimationState clip = dahan.GetComponent<Animation>()["walk"];
        dahan.GetComponent<Animation>().Play("walk");
        StartCoroutine("WenduXunhuan");
    }

    void InitParameter()
    {
        JoyPac.Instance().GA_TrackEvent("enter_level", currentLv);
        wenduZhongxin = Random.Range(40, 80);
        wenduRand = 2 + currentLv;

        slider.transform.Find("LV1/Text").GetComponent<Text>().text = currentLv.ToString();
        slider.transform.Find("LV3/Text").GetComponent<Text>().text = (currentLv + 1).ToString();

        if (wenduRand > 8)
            wenduRand = 8;
        rangeZhongxin = 30 - (currentLv * 2);
        if (rangeZhongxin < 8)
            rangeZhongxin = 8;
        interval = 13 - currentLv * 2;
        if (interval < 8)
            interval = 8;
        tangchi.manyiduDelta = 0.003f - 0.0003f * currentLv;
        if (tangchi.manyiduDelta <= 0.001f)
            tangchi.manyiduDelta = 0.001f;
        switch (currentLv % 3)
        {
            case 0:
                dahan = dahanList[2];
                matDahan = matList[2];
                matHan = hanList[2];
                dahanList[0].SetActive(false);
                dahanList[1].SetActive(false);
                dahanList[2].SetActive(true);
                break;
            case 1:
                dahan = dahanList[1];
                matDahan = matList[1];
                matHan = hanList[1];
                dahanList[0].SetActive(false);
                dahanList[1].SetActive(true);
                dahanList[2].SetActive(false);
                break;
            case 2:
                dahan = dahanList[0];
                matDahan = matList[0];
                matHan = hanList[0];
                dahanList[0].SetActive(true);
                dahanList[1].SetActive(false);
                dahanList[2].SetActive(false);
                break;
        }
    }

    bool countdown = false;
    int isColdOrHot = 0;
    public void StartCountDown(int i, int lastTime = 8)
    {
        isColdOrHot = i;
        if (i != 0 && !countdown)
        {
            if (i == -1)
            {
                StopCoroutine("QiPaoZi");
                StartCoroutine("QiPaoZi", "Cold");
            }
            if(i == 1)
            {
                StopCoroutine("QiPaoZi");
                StartCoroutine("QiPaoZi", "Hot");
            }
            effectAixin.SetActive(false);
            StartCoroutine("CountDown", (i == 1));
            countdown = true;
        }
        else if(i == 0 && countdown)
        {
            effectAixin.SetActive(true);
            StopCoroutine("CountDown");
            StopCoroutine("QiPaoZi");
            countdown = false;
        }
    }

    IEnumerator QiPaoZi(string s)
    {
        for(int i = 0; i < 3; i ++)
        {
            yield return new WaitForSeconds(Random.Range(2, 3));
            Color c = s == "Cold" ? Color.blue : Color.red;
            SetQiPao(s, c);
        }
    }

    IEnumerator CountDown(bool isRe)
    {
        int lastTime = 8;
        Color color = isRe ? Color.red : Color.blue;
        for (int i = 0; i < lastTime; i ++)
        {
            if (lastTime - i <= 5)
            {                
                ShowCountDown(lastTime - i, color);
            }
            yield return new WaitForSeconds(1);
        }
        PauseGame();
        JoyPac.Instance().GA_TrackEvent("fail_level", currentLv);
        uicontroller.OpenUIFailed();
    }

    void ShowCountDown(int i, Color color)
    {
        txtCountDown.GetComponent<Text>().text = i.ToString();
        txtCountDown.GetComponent<Text>().color = color;
        var tSequence = DOTween.Sequence();
        tSequence.Append(txtCountDown.transform.DOScale(new Vector3(1.3f, 1.3f, 1.3f), 0.1f))
            .Join(txtCountDown.GetComponent<Text>().DOFade(1f, 0.1f))
            .Append(txtCountDown.transform.DOScale(Vector3.one, 0.1f))
            .AppendInterval(0.5f)
            .Append(txtCountDown.transform.DOScale(Vector3.zero, 0.1f))
            .Join(txtCountDown.GetComponent<Text>().DOFade(0f, 0.1f));
    }

    IEnumerator WenduXunhuan()
    {
        dahan.GetComponent<Animation>().Play("walk");
        yield return new WaitForSeconds(3.5f);
        dahan.GetComponent<Animation>().Play("standby");
        sex.SetActive(true);
        water.SetActive(true);
        tangchi.isGameStart = true;
        while (tangchi.isGameStart)
        {
            wendu = wenduZhongxin + Random.Range(-wenduRand, wenduRand);
            range = (int)Random.Range(rangeZhongxin * 1.3f, rangeZhongxin * 0.7f);
            //SetQiPao(wendu.ToString() + "°C", Color.black);
            wenduji.InitWenduji(wendu, range);
            yield return new WaitForSeconds(Random.Range(interval * 0.7f, interval * 1.3f));
        }
    }

    void SetQiPao(string content, Color color)
    {
        qipao.transform.Find("Text").GetComponent<Text>().text = content;
        qipao.transform.Find("Text").GetComponent<Text>().color = color;
        var mSequence = DOTween.Sequence();
        mSequence.Append(qipao.transform.DOScale(new Vector3(1.3f, 1.3f, 1.3f), 0.2f))
            .Join(qipao.GetComponent<Image>().DOFade(1f, 0.3f))
            .Append(qipao.transform.DOScale(Vector3.one, 0.2f))
            .AppendInterval(2.5f)
            .Append(qipao.transform.DOScale(Vector3.zero, 0.3f))
            .Join(qipao.GetComponent<Image>().DOFade(0f, 0.3f));
    }
}